import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:kxy_flutter_app/r.dart';
import 'package:kxy_flutter_app/view/examination/view_model/exam_model.dart';
import 'package:provider/provider.dart';
import 'question_card_widget.dart';

class ExamBottomBarWidget extends StatelessWidget {
  final SwiperController swiperCtrl;
  final Function submitPaper;

  const ExamBottomBarWidget({
    Key key,
    @required this.swiperCtrl,
    this.submitPaper,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Consumer<ExamModel>(
      builder: (cxt, model, child) {
        return Container(
          height: 50,
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                blurRadius: 1,
                offset: Offset(0, -1),
                color: Color.fromRGBO(240, 240, 240, 1),
              ),
            ],
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(child: btnPre(model, context)),
              btnCollect(model, context),
              btnQuestionCard(model, context),
              btnSubmit(model, context),
              Expanded(child: btnNext(model, context)),
            ],
          ),
        );
      },
    );
  }

  Border border() => Border(
        right: BorderSide(
          color: Color.fromRGBO(240, 240, 240, 1),
        ),
      );

  Widget btnPre(ExamModel model, BuildContext context) {
    var isBegin = model.index <= 0;
    var color = isBegin ? Colors.black54 : Colors.black;
    return GestureDetector(
      child: Container(
        decoration: BoxDecoration(
          border: border(),
          color: Colors.white,
        ),
        height: 50,
        alignment: Alignment.center,
        child: Text(
          '上一题',
          style: TextStyle(
            fontSize: 14,
            color: color,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
      onTap: () {
        if (isBegin) return;
        swiperCtrl.previous();
      },
    );
  }

  Widget btnNext(ExamModel model, BuildContext context) {
    var isEnd = model.index >= model.questionList.length - 1;
    var color = isEnd ? Colors.black54 : Colors.black;
    return GestureDetector(
      child: Container(
        height: 50,
        color: Colors.white,
        alignment: Alignment.center,
        child: Text(
          '下一题',
          style: TextStyle(
            fontSize: 14,
            color: color,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
      onTap: () {
        if (isEnd) return;
        swiperCtrl.next();
      },
    );
  }

  Widget btnCollect(ExamModel model, BuildContext context) {
    var isCollect = model.current.isCollect;
    return GestureDetector(
      child: Container(
        height: 50,
        width: 60,
        decoration: BoxDecoration(border: border()),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.only(bottom: 3),
              child: Image.asset(
                isCollect ? R.assetsCommonCollect : R.assetsCommonUncollect,
                height: 20,
              ),
            ),
            Text(
              '收藏',
              style: TextStyle(
                fontSize: 11,
                color: Colors.black54,
              ),
            ),
          ],
        ),
      ),
      onTap: model.collect,
    );
  }

  Widget btnSubmit(ExamModel model, BuildContext context) {
    return GestureDetector(
      child: Container(
          decoration: BoxDecoration(border: border()),
          height: 50,
          width: 60,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Padding(
                padding: EdgeInsets.only(bottom: 3),
                child: Image.asset(
                  R.assetsExamSubmitPaper,
                  height: 20,
                ),
              ),
              Text(
                '交卷',
                style: TextStyle(
                  fontSize: 11,
                  color: Colors.black54,
                ),
              ),
            ],
          )),
      onTap: submitPaper,
    );
  }

  Widget btnQuestionCard(ExamModel model, BuildContext context) {
    return GestureDetector(
      child: Container(
        decoration: BoxDecoration(border: border()),
        height: 50,
        width: 60,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.only(bottom: 3),
              child: Image.asset(
                R.assetsExamQuesCard,
                height: 20,
              ),
            ),
            Text(
              '答题卡',
              style: TextStyle(
                fontSize: 11,
                color: Colors.black54,
              ),
            ),
          ],
        ),
      ),
      onTap: () {
        showDialog(
          context: context,
          builder: (cxt) {
            return QuestionCardView(
              onSelect: (index) => swiperCtrl.move(index, animation: false),
              questionList: model.questionList,
            );
          },
        );
      },
    );
  }
}
